package cn.qzuie.smart.module.dorm.controller.admin.assettype;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.qzuie.smart.framework.common.pojo.PageParam;
import cn.qzuie.smart.framework.common.pojo.PageResult;
import cn.qzuie.smart.framework.common.pojo.CommonResult;
import cn.qzuie.smart.framework.common.util.object.BeanUtils;
import static cn.qzuie.smart.framework.common.pojo.CommonResult.success;

import cn.qzuie.smart.framework.excel.core.util.ExcelUtils;

import cn.qzuie.smart.framework.apilog.core.annotation.ApiAccessLog;
import static cn.qzuie.smart.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.qzuie.smart.module.dorm.controller.admin.assettype.vo.*;
import cn.qzuie.smart.module.dorm.dal.dataobject.assettype.AssetDO;
import cn.qzuie.smart.module.dorm.service.assettype.AssetService;

@Tag(name = "管理后台 - 资产")
@RestController
@RequestMapping("/dorm/asset")
@Validated
public class AssetController {

    @Resource
    private AssetService assetService;

    @PostMapping("/create")
    @Operation(summary = "创建资产")
    @PreAuthorize("@ss.hasPermission('dorm:asset:create')")
    public CommonResult<Long> createAsset(@Valid @RequestBody AssetSaveReqVO createReqVO) {
        return success(assetService.createAsset(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产")
    @PreAuthorize("@ss.hasPermission('dorm:asset:update')")
    public CommonResult<Boolean> updateAsset(@Valid @RequestBody AssetSaveReqVO updateReqVO) {
        assetService.updateAsset(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dorm:asset:delete')")
    public CommonResult<Boolean> deleteAsset(@RequestParam("id") Long id) {
        assetService.deleteAsset(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除资产")
                @PreAuthorize("@ss.hasPermission('dorm:asset:delete')")
    public CommonResult<Boolean> deleteAssetList(@RequestParam("ids") List<Long> ids) {
        assetService.deleteAssetListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dorm:asset:query')")
    public CommonResult<AssetRespVO> getAsset(@RequestParam("id") Long id) {
        AssetDO asset = assetService.getAsset(id);
        return success(BeanUtils.toBean(asset, AssetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产分页")
    @PreAuthorize("@ss.hasPermission('dorm:asset:query')")
    public CommonResult<PageResult<AssetRespVO>> getAssetPage(@Valid AssetPageReqVO pageReqVO) {
        PageResult<AssetDO> pageResult = assetService.getAssetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产 Excel")
    @PreAuthorize("@ss.hasPermission('dorm:asset:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAssetExcel(@Valid AssetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AssetDO> list = assetService.getAssetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资产.xls", "数据", AssetRespVO.class,
                        BeanUtils.toBean(list, AssetRespVO.class));
    }

}